package com.example.SRT_Editor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import com.example.SRT_Editor.R;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;

import android.view.View;
import android.widget.TextView;	

public class SRTSplit_wndAct extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.wind2);
	}
	private File file1=null,file2=null;
	private int EOF=1;

	public void process(View v){
		TextView t1= (TextView) findViewById(R.id.CD0);
		String path = t1.getText().toString();

		File sdcard = Environment.getExternalStorageDirectory();

		file1 = new File(sdcard,path);
		file2 = new File(sdcard,path.replace(".srt", "1.srt"));
		try {
		if (file2.exists()) 
			file2.delete();
		file2.createNewFile();
		}
		catch(IOException e){
			e.printStackTrace();
		}
		TextView t= (TextView) findViewById(R.id.editText2);	
		long pivot=0;
		String Line=null;
		TextView lb=(TextView) findViewById(R.id.ld1);
		int i=1;
		try{
			
			BufferedReader br1 = new BufferedReader(new FileReader(file1));
			pivot=toMillsec(t.getText().toString());
			FileWriter out = new FileWriter(file2, true);	
			while((Line=br1.readLine())!=null){
				if(Line.contains("-->")){
					EOF++;
					if(toMillsec(Line.substring(0, 12))>=pivot)
						{out.append(Integer.valueOf(i).toString());
						out.append("\n");
						Line=toStrng(toMillsec(Line.substring(0, 12))-pivot)+" --> "+toStrng(toMillsec(Line.substring(17))-pivot);
						out.append(Line);
						out.append("\n");
						break;
						}
					else if(toMillsec(Line.substring(17))>=pivot){
						out.append(Integer.valueOf(i).toString());
						out.append("\n");
						Line=toStrng(pivot)+" --> "+toStrng(toMillsec(Line.substring(17))-pivot);
						out.append(Line);
						out.append("\n");
						break;
					}			

				}
			}
			
			i++;
			
			while((Line=br1.readLine())!=null){
				if(Line.equals(Integer.valueOf(EOF).toString())){

					out.append(Integer.valueOf(i).toString());
					out.append("\n");
					Line=br1.readLine();
					Line=toStrng(toMillsec(Line.substring(0, 12))-pivot)+" --> "+toStrng(toMillsec(Line.substring(17))-pivot);
					out.append(Line);
					out.append("\n");
					i++;
					EOF++;
				}
				else
				{
					out.append(Line);
					out.append("\n");
				}

			}
			br1.close();
			out.close();
			lb.setText("Success");
		}
		catch(Exception e){
			e.printStackTrace();
			lb.setText("Failed! Error Encountered");		
		}	
//		catch(ArithmeticException e){
//			e.printStackTrace();
//			lb.setText("Failed! Error Encountered");		
//		}
	}



	private long toMillsec(String line) throws Exception{
		int h,m,s,ms;
		h=m=s=ms=0;
		h=Integer.parseInt(line.substring(0, 2));
		m=Integer.parseInt(line.substring(3, 5));
		s=Integer.parseInt(line.substring(6, 8));
		ms=Integer.parseInt(line.substring(9,12));
		return ((h*60+m)*60+s)*1000 +ms;
	}

	private String toStrng(long i) throws Exception{
		String t=null;
		int ms = (int) i % 1000;
		i /= 1000;
		int s = (int) i % 60;
		i /= 60;
		int m = (int) i % 60;
		int h = (int) i/60;
		
		if(h<10) t="0"+Integer.valueOf(h).toString();
		else t=Integer.valueOf(h).toString();
		
		if(m<10) t += ":0"+Integer.valueOf(m).toString();
		else t +=":"+Integer.valueOf(m).toString();
	
		if(s<10) t += ":0"+Integer.valueOf(s).toString();
		else t +=":"+Integer.valueOf(s).toString();
	
		if(ms<10) t += ",00"+Integer.valueOf(ms).toString();
		else if(ms<100) t += ",0"+Integer.valueOf(ms).toString();
		else t +=","+Integer.valueOf(ms).toString();
		
		return t;

	}
	
	
}
